<template>
  <div class="login">
    <!-- 完成结构 -->
    <el-form :model="form" class="myform" :rules="rules" ref="form">
      <h3>iHRM 后台登录系统</h3>
      <el-form-item prop="mobile">
        <el-input
          v-model="form.mobile"
          prefix-icon="el-icon-user-solid"
          placeholder="请输入手机"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="form.password"
          show-password
          prefix-icon="el-icon-lock"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button class="btn" type="primary" :loading="loading" @click="login"
          >登录</el-button
        >
        <h6>还没有账号？立即注册</h6>
      </el-form-item>
    </el-form>
  </div>
</template>


<script>
import { loginApi } from "@/api/user.js";
import { mapActions } from "vuex";
import { Message } from "element-ui";
export default {
  data() {
    return {
      form: {
        mobile: "13800000002", // 手机号
        password: "123456", // 密码
      },
      loading: false,
      rules: {
        mobile: [
          { required: true, message: "请输入手机号码", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              // 定义手机号的正则
              const res =
                /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;
              // 测试数据
              const bool = res.test(value);
              bool ? callback() : callback(new Error("手机号不合法"));
            },
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 4,
            max: 10,
            message: "长度在 4 到 10 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    // 导入辅助函数
    ...mapActions({
      userLogin: "user/login",
    }),
    // 提示登录的参数
    login() {
      // 完成全局的表单校验
      this.$refs.form.validate(async (result) => {
        if (result) {
          // 发送请求
          // const res = await loginApi(this.form)
          // // 接收响应
          // console.log(res)
          try {
            this.loading = true;
            await this.userLogin(this.form);
            this.$router.push("/");
            this.$message({
              message: "欢迎回家",
              type: "success",
            });
            // 判断当前路径是否有参数：_redirect
            const _redirect = this.$route.query._redirect;
            if (_redirect) {
              // 跳转到_redirect 对应的页面
              this.$router.push(_redirect);
            } else {
              // 没有的话，直接跳转到首页
              this.$router.push("/");
            }
          } catch (error) {
            console.log(error);
          } finally {
            this.loading = false;
          }
        } else {
          console.log("验证失败");
        }
      });
    },
  },
};
</script>


<style lang="scss">
.login {
  background-image: url("../../assets/common/login.jpg");
  background-position: center;
  height: 100%;
  .myform {
    width: 520px;
    height: 400px;
    padding: 36px;
    padding-top: 0px;
    box-sizing: border-box;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -70%);
    h3 {
      font-size: 43px;
      font-weight: 150;
      letter-spacing: 6px; // 设置文本之间的间隔
      color: #fff;
      line-height: 1;
    }
    h6 {
      font-size: 14px;
      line-height: 14px;
      text-align: center;
      font-weight: 150px;
      color: #fff;
    }
    // 设置输入框的高度
    input.el-input__inner {
      height: 50px;
      background-color: rgba(255, 255, 255, 0.7);
      border: none;
      color: #68b0fe;
      border-radius: 6px;
      padding-left: 40px;
    }
    i.el-input__icon {
      color: #666;
    }
    .btn {
      width: 100%;
      height: 60px;
      font-size: 24px;
      background-color: #407ffe;
      border-radius: 6px;
    }
    .el-form-item__error {
      color: #fff;
    }
  }
}
</style>